﻿namespace EFS.RC.CC.Applications.Remote.WallBoard
{
	/// <summary>
	/// WallBoard session
	/// </summary>
	internal class Monitor : EFS.RC.CC.Applications.Remote.Application
	{
		#region Composition

		// sync access
		private object syncAccess = new object();

		// Office service end point
		private EFS.RC.CC.Applications.EndPoints.OfficeService.Operators operatorOfficeServiceEndPoint = null;

		// Wall board filter
		private System.Collections.Specialized.StringCollection fileterCustomerOfficeServices = new System.Collections.Specialized.StringCollection();

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="wallBoardInstantMessagingCall"></param>
		/// <param name="operatorOfficeServiceEndPoint"></param>
		public Monitor(Microsoft.Rtc.Collaboration.InstantMessagingCall wallBoardInstantMessagingCall, EFS.RC.CC.Applications.EndPoints.OfficeService.Operators operatorOfficeServiceEndPoint) 
			: base(wallBoardInstantMessagingCall)
		{
			// check
			EFS.Common.Verify.Reference(wallBoardInstantMessagingCall, "wallBoardInstantMessagingCall");
			EFS.Common.Verify.Reference(operatorOfficeServiceEndPoint, "operatorOfficeServiceEndPoint");

			// set internals
			this.operatorOfficeServiceEndPoint = operatorOfficeServiceEndPoint;

			// log
			EFS.EventLog.Log.LogInfo("WallBoard", this);
		}

		#endregion

		#region Usage

		/// <summary>
		/// Describe this object into a nice formated string
		/// </summary>
		/// <returns></returns>
		public override System.String Describe()
		{
			return System.String.Format("EFS.RC.CC.Applications.Remote.WallBoard [{0}]", base.Describe());
		}

		#endregion

		#region Startup

		/// <summary>
		/// OnEstablished
		/// </summary>
		protected override void OnEstablished()
		{
			EFS.EventLog.Log.LogInfo("OnEstablished", this);

			// register wall board for messages
			operatorOfficeServiceEndPoint.OfficeServiceTracker.RegisterWallBoardApplication(this);

			// send monitored apps
			SendMonitoredApplications();
		}

		#endregion

		#region Runtime

		/// <summary>
		/// Send monitored applications list
		/// </summary>
		private void SendMonitoredApplications()
		{
			operatorOfficeServiceEndPoint.ApplicationPresence.EndPointsPresencesRegistrar.ForeachCustomerOfficeServicePresence(SendMonitoredApplication);
		}

		/// <summary>
		/// Send monitored applications list
		/// </summary>
		private void SendMonitoredApplication(EFS.RC.CC.Presences.EndPoints.OfficeService.Customer customersOfficeServiceEndPointPresence)
		{
			// check
			EFS.Common.Verify.Reference(customersOfficeServiceEndPointPresence, "customersOfficeServiceEndPointPresence");

			// check if active
			if (null != customersOfficeServiceEndPointPresence.PresenceEndPointApplication)
			{
				// make new message
				EFS.RC.CC.Messages.Application.CustomerEndPoint customerEndPointwallBoardMessage = new EFS.RC.CC.Messages.Application.CustomerEndPoint();

				// set
				customerEndPointwallBoardMessage.PhoneUri = customersOfficeServiceEndPointPresence.PresenceEndPointDeployment.URI;
				customerEndPointwallBoardMessage.RequiredSkillId = customersOfficeServiceEndPointPresence.PresenceEndPointDeployment.SkillID.Value;

				// send
				BeginDataSend(customerEndPointwallBoardMessage);
			}
		}

		/// <summary>
		/// OnMessageReceived
		/// </summary>
		/// <param name="message"></param>
		protected override void OnMessageReceived(EFS.RC.CC.Messages.Message message)
		{ }

		#endregion // Runtime

		#region Shutdown

		#endregion
	}
}

